﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	public class ArticleWithAdvertList :ArticleList
	{
		private LoadedValue<string> loadedLinkClassCode;
		public virtual string LinkClassCode
		{
			get
			{
				if (loadedLinkClassCode.Loader == null)
					loadedLinkClassCode.Loader = () => (string)ViewState["LinkClassCode"];
				return loadedLinkClassCode.Value;
			}
			set
			{
				if (loadedLinkClassCode.Setter == null)
					loadedLinkClassCode.Setter = v => ViewState["LinkClassCode"] = v;
				loadedLinkClassCode.Value = value;
			}
		}

		private LoadedValue<string> loadedLinkResourceCopyName;
		public virtual string LinkResourceCopyName
		{
			get
			{
				if (loadedLinkResourceCopyName.Loader == null)
					loadedLinkResourceCopyName.Loader = () => (string)ViewState["LinkResourceCopyName"];
				return loadedLinkResourceCopyName.Value;
			}
			set
			{
				if (loadedLinkResourceCopyName.Setter == null)
					loadedLinkResourceCopyName.Setter = v => ViewState["LinkResourceCopyName"] = v;
				loadedLinkResourceCopyName.Value = value;
			}
		}

		private LoadedValue<int> loadedLinkIndex;
		public virtual int LinkIndex
		{
			get
			{
				if (loadedLinkIndex.Loader == null)
					loadedLinkIndex.Loader = () => IntExtensions.ConvertFromOrDefault(ViewState["LinkIndex"], 1);
				return loadedLinkIndex.Value;
			}
			set
			{
				if (loadedLinkIndex.Setter == null)
					loadedLinkIndex.Setter = v => ViewState["LinkIndex"] = v;
				loadedLinkIndex.Value = value;
			}
		}
		private LoadedValue<bool> loadedShowDefaultUrl;
		public bool ShowDefaultUrl
		{
			get
			{
				if (loadedShowDefaultUrl.Loader == null)
					loadedShowDefaultUrl.Loader = () => BooleanExtensions.ParseOrDefault((string)ViewState["ShowDefaultUrl"], true);
				return loadedShowDefaultUrl.Value;
			}
			set
			{
				if (loadedShowDefaultUrl.Setter == null)
					loadedShowDefaultUrl.Setter = v => ViewState["ShowDefaultUrl"] = v;
				loadedShowDefaultUrl.Value = value;
			}
		}

		private LoadedValue<string> loadedLinkTitle;
		public virtual string LinkTitle
		{
			get {
				if (loadedLinkTitle.Loader == null)
					loadedLinkTitle.Loader = () => (string)ViewState["LinkTitle"];
				return loadedLinkTitle.Value;
			}
			set {
				if (loadedLinkTitle.Setter == null)
					loadedLinkTitle.Setter = v => ViewState["LinkTitle"] = v;
				loadedLinkTitle.Value = value;
			}
		}

		private LoadedValue<string> loadedLinkContent;
		public virtual string LinkContent
		{
			get
			{
				if (loadedLinkContent.Loader == null)
					loadedLinkContent.Loader = () => (string)ViewState["LinkContent"];
				return loadedLinkContent.Value;
			}
			set
			{
				if (loadedLinkContent.Setter == null)
					loadedLinkContent.Setter = v => ViewState["LinkContent"] = v;
				loadedLinkContent.Value = value;
			}
		}


		public virtual string LinkImageTag
		{
			get
			{
				List<Links.LinkForImageList> links = Links.GetPaginationImageList(SiteId, LinkClassCode, 1, LinkIndex).Records;
				if (links == null || links.Count == 0) return null;
				Links.LinkForImageList link = links[0];

				string resource = link.ImageType;
				if (string.IsNullOrEmpty(resource)) return "";

				LinkTitle = link.Title;
				LinkContent = link.Describe;

				string linkUrl = "";

				if (!string.IsNullOrEmpty(link.LinkUrl))
				{
					if (ShowDefaultUrl)
					{
						if (link.LinkUrl.Contains("http") || link.LinkUrl.Trim().IndexOf('/') == 0)
							linkUrl = link.LinkUrl;
						else
							linkUrl = string.Format("http://{0}", link.LinkUrl);
					}
					else
					{
						linkUrl = string.Format(@"/Link_{0}.htm", link.LinkId);
					}
				}
				if (resource.EndsWith(".swf", StringComparison.CurrentCultureIgnoreCase))
				{
					ResourceThumbnailCopyElement copyConfig = ApplicationConfiguration.GetResourceThumbnailCopy(LinkResourceCopyName);
					return @"<embed width=""" + copyConfig.Width + @"px"" height=""" + copyConfig.Height + @"px"" name=""plugin"" src=""" + GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(resource, null) + @""" type=""application/x-shockwave-flash"" />";
				}

				if (OpenWithDefauleWay)
					return string.Format(@"<a class=""p1"" href=""{0}"" target=""_blank""><img src=""{1}"" alt=""{2}"" /></a>", linkUrl, GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(resource, LinkResourceCopyName), link.Title);
				return string.Format(@"<a class=""p1"" onclick=""window.open('{0}')"" href=""javaScript:void(0);""><img src=""{1}"" alt=""{2}"" /></a>", linkUrl, GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(resource, LinkResourceCopyName), link.Title);
			}
		}
	}
}
